// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten. You can (and have to!) safely modify it by hand.
 */
package org.tinaelle.server.service;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.tinaelle.ServiceLocator;
import org.tinaelle.common.dto.AdColumnDTO;
import org.tinaelle.common.dto.AdFieldDTO;
import org.tinaelle.common.dto.AdScreenDTO;
import org.tinaelle.common.dto.AdTabDTO;
import org.tinaelle.common.dto.AdTableDTO;
import org.tinaelle.server.domain.AdField;
import org.tinaelle.server.domain.AdFieldDao;
import org.tinaelle.server.domain.AdScreen;
import org.tinaelle.server.domain.AdScreenDao;
import org.tinaelle.server.domain.AdTab;
import org.tinaelle.server.domain.AdTabDao;
import org.tinaelle.utils.BootstrapUtils;

/**
 * @see org.tinaelle.server.service.AdWindowService
 */
public class AdWindowServiceImpl extends org.tinaelle.server.service.AdWindowServiceBase {
	private final Logger	logger	= Logger.getLogger( this.getClass() );

	@Override
	protected AdScreenDTO handleGetScreenByName(final String name) throws Exception {
		return ( AdScreenDTO ) getAdScreenDao().findByName( AdScreenDao.TRANSFORM_ADSCREENDTO, name );
	}

	/**
	 * @see org.tinaelle.server.service.AdWindowService#fillInitialValues()
	 */
	@Override
	protected void handleFillInitialValues() throws java.lang.Exception {
		// TODO implement protected void handleFillInitialValues()
		throw new java.lang.UnsupportedOperationException(
		        "org.tinaelle.server.service.AdWindowService.handleFillInitialValues() Not implemented!" );
	}

	@Override
	protected void handleFillInitialValuesForScreen() throws Exception {
		final ServiceLocator serviceLocator = ServiceLocator.instance();
		final AdSystemService adSystemSrv = serviceLocator.getAdSystemService();

		// File Format :
		// Name, Description, Entity Type : 0, 1, 2
		String[][] screenContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdScreen" );
		for ( int i = 1; i < screenContent.length; i++ ) {
			final String[] screenLine = screenContent[ i ];

			if ( screenLine.length > 1 ) {
				AdScreenDTO adScreenDTO = getScreenByName( screenLine[ 0 ] );
				if ( adScreenDTO == null ) {
					adScreenDTO = new AdScreenDTO();
				}

				adScreenDTO.setName( screenLine[ 0 ] );
				adScreenDTO.setDescription( screenLine[ 1 ] );
				adScreenDTO.setAdEntityTypeId( adSystemSrv.getEntityTypeByName( screenLine[ 2 ].trim() )
				        .getAdEntityTypeId() );
				adScreenDTO = save( adScreenDTO );
			}
		}
	}

	@Override
	protected void handleFillInitialValuesForField() throws Exception {
		final ServiceLocator serviceLocator = ServiceLocator.instance();
		final AdSystemService adSystemSrv = serviceLocator.getAdSystemService();
		final AdTextService adTextSrv = serviceLocator.getAdTextService();

		// File Format :
		// Name, Ref Tab Name, Ref Screen Name : 0, 1, 2
		// Ref Ad Element, Ref Ad Column, Ref Ad Reference : 3, 4, 5
		// Ref Ad Reference Value, Description, Help : 6, 7, 8
		// Is Centrally Maintained, Read Only Logic Form, Display Logic Form : 9, 10, 11
		// Read Only Logic Grid, Display Logic Grid, Is Same Line : 12, 13, 14
		// X, Y, Display Length Form : 15, 16, 17
		// Display Length Grid, Colspan, Rowspan : 18, 19, 20
		// Is Mandatory, Seq No Form, Seq No Grid : 21, 22, 23
		// Is Field Only, Is Heading, Default Value : 24, 25, 26
		// Sort No, Ref Ad Val Rule, Ref Entity Type : 27, 28, 29
		String[][] fieldContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdField" );
		for ( int i = 1; i < fieldContent.length; i++ ) {
			final String[] fieldLine = fieldContent[ i ];

			if ( fieldLine.length > 1 ) {
				AdScreenDTO adScreenDTO = getScreenByName( fieldLine[ 2 ].trim() );
				AdTabDTO adTabDTO = getTabByName( adScreenDTO, fieldLine[ 1 ].trim() );
				AdFieldDTO adFieldDTO = getFieldByName( adTabDTO, fieldLine[ 0 ].trim() );
				AdTableDTO adTableDTO = adSystemSrv.getTableById( adTabDTO.getAdTableId() );
				AdColumnDTO adColumnDTO = adSystemSrv.getColumnByColumnName( adTableDTO, fieldLine[ 4 ].trim()
				        .toUpperCase() );
				if ( adFieldDTO == null ) {
					adFieldDTO = new AdFieldDTO();
				}

				adFieldDTO.setName( fieldLine[ 0 ].trim() );
				adFieldDTO.setAdTabId( adTabDTO.getAdTabId() );
				if ( ( fieldLine[ 3 ] != null ) && ( fieldLine[ 3 ].trim().length() > 0 ) ) {
					adFieldDTO.setAdElementId( adTextSrv.getElementByName( fieldLine[ 3 ].trim() ).getAdElementId() );
				}
				adFieldDTO.setAdColumnId( adColumnDTO.getAdColumnId() );
				if ( ( fieldLine[ 5 ] != null ) && ( fieldLine[ 5 ].trim().length() > 0 ) ) {
					adFieldDTO.setAdReferenceId( adSystemSrv.getReferenceByInternalName( fieldLine[ 5 ].trim() )
					        .getAdReferenceId() );
				} else {
					adFieldDTO.setAdReferenceId( adColumnDTO.getAdReferenceId() );
				}
				if ( ( fieldLine[ 6 ] != null ) && ( fieldLine[ 6 ].trim().length() > 0 ) ) {
					adFieldDTO.setAdReferenceValueId( adSystemSrv.getReferenceByInternalName( fieldLine[ 6 ].trim() )
					        .getAdReferenceId() );
				} else {
					adFieldDTO.setAdReferenceValueId( adColumnDTO.getAdReferenceValueId() );
				}
				adFieldDTO.setDescription( fieldLine[ 7 ].trim() );
				adFieldDTO.setHelp( fieldLine[ 8 ].trim().length() == 0 ? null : fieldLine[ 8 ].trim() );
				adFieldDTO.setIsCentrallyMaintained( fieldLine[ 9 ].trim().equalsIgnoreCase( "TRUE" )
				        || fieldLine[ 9 ].trim().equalsIgnoreCase( "Y" )
				        || fieldLine[ 9 ].trim().equalsIgnoreCase( "1" ) );
				adFieldDTO.setReadOnlyLogicForm( fieldLine[ 10 ].trim().length() == 0 ? null : fieldLine[ 10 ].trim() );
				adFieldDTO.setDisplayLogicForm( fieldLine[ 11 ].trim().length() == 0 ? null : fieldLine[ 11 ].trim() );
				adFieldDTO.setReadOnlyLogicGrid( fieldLine[ 12 ].trim().length() == 0 ? null : fieldLine[ 12 ].trim() );
				adFieldDTO.setDisplayLogicGrid( fieldLine[ 13 ].trim().length() == 0 ? null : fieldLine[ 13 ].trim() );
				adFieldDTO.setIsSameLine( fieldLine[ 14 ].trim().equalsIgnoreCase( "TRUE" )
				        || fieldLine[ 14 ].trim().equalsIgnoreCase( "Y" )
				        || fieldLine[ 14 ].trim().equalsIgnoreCase( "1" ) );
				if ( ( fieldLine[ 15 ] != null ) && ( fieldLine[ 15 ].trim().length() > 0 ) ) {
					adFieldDTO.setX( Integer.parseInt( fieldLine[ 15 ].trim() ) );
				}
				if ( ( fieldLine[ 16 ] != null ) && ( fieldLine[ 16 ].trim().length() > 0 ) ) {
					adFieldDTO.setY( Integer.parseInt( fieldLine[ 16 ].trim() ) );
				}
				if ( ( fieldLine[ 17 ] != null ) && ( fieldLine[ 17 ].trim().length() > 0 ) ) {
					adFieldDTO.setDisplayLengthForm( Integer.parseInt( fieldLine[ 17 ].trim() ) );
				} else {
					adFieldDTO.setDisplayLengthForm( adColumnDTO.getFieldLength() );
				}

				if ( ( fieldLine[ 18 ] != null ) && ( fieldLine[ 18 ].trim().length() > 0 ) ) {
					adFieldDTO.setDisplayLengthGrid( Integer.parseInt( fieldLine[ 18 ].trim() ) );
				} else {
					adFieldDTO.setDisplayLengthGrid( adColumnDTO.getFieldLength() );
				}
				if ( ( fieldLine[ 19 ] != null ) && ( fieldLine[ 19 ].trim().length() > 0 ) ) {
					adFieldDTO.setColSpan( Integer.parseInt( fieldLine[ 19 ].trim() ) );
				}
				if ( ( fieldLine[ 20 ] != null ) && ( fieldLine[ 20 ].trim().length() > 0 ) ) {
					adFieldDTO.setRowSpan( Integer.parseInt( fieldLine[ 20 ].trim() ) );
				}
				if ( ( fieldLine[ 21 ] != null ) && ( fieldLine[ 21 ].trim().length() > 0 ) ) {
					adFieldDTO.setIsMandatory( fieldLine[ 21 ].trim().equalsIgnoreCase( "TRUE" )
					        || fieldLine[ 21 ].trim().equalsIgnoreCase( "Y" )
					        || fieldLine[ 21 ].trim().equalsIgnoreCase( "1" ) );
				} else {
					adFieldDTO.setIsMandatory( adColumnDTO.getIsMandatory() );
				}
				adFieldDTO.setSeqNoForm( Integer.parseInt( fieldLine[ 22 ].trim() ) );
				adFieldDTO.setSeqNoGrid( Integer.parseInt( fieldLine[ 23 ].trim() ) );
				adFieldDTO.setIsFieldOnly( fieldLine[ 24 ].trim().equalsIgnoreCase( "TRUE" )
				        || fieldLine[ 24 ].trim().equalsIgnoreCase( "Y" )
				        || fieldLine[ 24 ].trim().equalsIgnoreCase( "1" ) );
				adFieldDTO.setIsHeading( fieldLine[ 25 ].trim().equalsIgnoreCase( "TRUE" )
				        || fieldLine[ 25 ].trim().equalsIgnoreCase( "Y" )
				        || fieldLine[ 25 ].trim().equalsIgnoreCase( "1" ) );
				adFieldDTO.setDefaultValue( fieldLine[ 26 ].trim() );
				if ( ( fieldLine[ 27 ] != null ) && ( fieldLine[ 27 ].trim().length() > 0 ) ) {
					adFieldDTO.setSortNo( Integer.parseInt( fieldLine[ 27 ].trim() ) );
				}
				if ( ( fieldLine[ 28 ] != null ) && ( fieldLine[ 28 ].trim().length() > 0 ) ) {
					adFieldDTO.setAdValRuleId( adSystemSrv.getValRuleByName( fieldLine[ 28 ].trim() ).getAdValRuleId() );
				} else {
					adFieldDTO.setAdValRuleId( adColumnDTO.getAdValRuleId() );
				}
				adFieldDTO.setAdEntityTypeId( adSystemSrv.getEntityTypeByName( fieldLine[ 29 ].trim() )
				        .getAdEntityTypeId() );

				adFieldDTO = save( adFieldDTO );
			}
		}
	}

	@Override
	protected void handleFillInitialValuesForFieldGroup() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	protected void handleFillInitialValuesForTab() throws Exception {
		final ServiceLocator serviceLocator = ServiceLocator.instance();
		final AdSystemService adSystemSrv = serviceLocator.getAdSystemService();

		// File Format :
		// Name, Ref Screen Name, Ref Table Name : 0, 1, 2
		// Description, Help, Read Only Logic : 3, 4, 5
		// SeqNo, Depth, Is Centrally Maintained : 6, 7, 8
		// Column Count Order By Clause, Where Clause : 9, 10, 11
		// Is Insert Record Is Translation Tab, Entity Type : 12, 13, 14
		String[][] tabContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdTab" );
		for ( int i = 1; i < tabContent.length; i++ ) {
			final String[] tabLine = tabContent[ i ];

			if ( tabLine.length > 1 ) {
				AdScreenDTO adScreenDTO = getScreenByName( tabLine[ 1 ].trim() );
				AdTabDTO adTabDTO = getTabByName( adScreenDTO, tabLine[ 0 ].trim() );

				if ( adTabDTO == null ) {
					adTabDTO = new AdTabDTO();
				}

				adTabDTO.setName( tabLine[ 0 ].trim() );
				adTabDTO.setAdScreenId( adScreenDTO.getAdScreenId() );
				adTabDTO.setAdTableId( adSystemSrv.getTableByTableName( tabLine[ 2 ].trim().toUpperCase() )
				        .getAdTableId() );
				adTabDTO.setDescription( tabLine[ 3 ].trim() );
				adTabDTO.setHelp( tabLine[ 4 ].trim().length() == 0 ? null : tabLine[ 4 ].trim() );
				adTabDTO.setReadOnlyLogic( tabLine[ 5 ].trim().length() == 0 ? null : tabLine[ 5 ].trim() );
				adTabDTO.setSeqNo( Integer.parseInt( tabLine[ 6 ].trim() ) );
				adTabDTO.setDepth( Integer.parseInt( tabLine[ 7 ].trim() ) );
				adTabDTO.setIsCentrallyMaintained( tabLine[ 8 ].trim().equalsIgnoreCase( "TRUE" )
				        || tabLine[ 8 ].trim().equalsIgnoreCase( "Y" ) || tabLine[ 8 ].trim().equalsIgnoreCase( "1" ) );
				adTabDTO.setColumnCount( Integer.parseInt( tabLine[ 9 ].trim() ) );
				adTabDTO.setOrderByClause( tabLine[ 10 ].trim().length() == 0 ? null : tabLine[ 10 ].trim() );
				adTabDTO.setWhereClause( tabLine[ 11 ].trim().length() == 0 ? null : tabLine[ 11 ].trim() );
				adTabDTO
				        .setIsInsertRecord( tabLine[ 12 ].trim().equalsIgnoreCase( "TRUE" )
				                || tabLine[ 12 ].trim().equalsIgnoreCase( "Y" )
				                || tabLine[ 12 ].trim().equalsIgnoreCase( "1" ) );
				adTabDTO
				        .setIsTranslationTab( tabLine[ 13 ].trim().equalsIgnoreCase( "TRUE" )
				                || tabLine[ 13 ].trim().equalsIgnoreCase( "Y" )
				                || tabLine[ 13 ].trim().equalsIgnoreCase( "1" ) );
				adTabDTO
				        .setAdEntityTypeId( adSystemSrv.getEntityTypeByName( tabLine[ 14 ].trim() ).getAdEntityTypeId() );

				adTabDTO = save( adTabDTO );
			}
		}
	}

	@Override
	protected AdFieldDTO[] handleGetFields(AdTabDTO adTabDTO) throws Exception {
		final AdTab adTab = ( AdTab ) getAdTabDao().load( AdTabDao.TRANSFORM_NONE, adTabDTO.getAdTabId() );
		final Collection<AdField> adFields = adTab.getAdFields();

		getAdFieldDao().toAdFieldDTOCollection( adFields );

		return adFields.toArray( new AdFieldDTO[ 0 ] );
	}

	@Override
	protected AdTabDTO[] handleGetTabs(AdScreenDTO adScreenDTO) throws Exception {
		final AdScreen adScreen = ( AdScreen ) getAdScreenDao().load( AdScreenDao.TRANSFORM_NONE,
		        adScreenDTO.getAdScreenId() );
		final Collection<AdTab> adTabs = adScreen.getAdTabs();

		getAdTabDao().toAdTabDTOCollection( adTabs );

		return adTabs.toArray( new AdTabDTO[ 0 ] );
	}

	@Override
	protected AdFieldDTO handleGetFieldByName(AdTabDTO adTabDTO, String name) throws Exception {
		AdTab adTab = ( AdTab ) getAdTabDao().load( AdTabDao.TRANSFORM_NONE, adTabDTO.getAdTabId() );
		return ( AdFieldDTO ) getAdFieldDao().findByName( AdFieldDao.TRANSFORM_ADFIELDDTO, adTab, name );
	}

	@Override
	protected AdTabDTO handleGetTabByName(AdScreenDTO adScreenDTO, String name) throws Exception {
		AdScreen adScreen = ( AdScreen ) getAdScreenDao()
		        .load( AdScreenDao.TRANSFORM_NONE, adScreenDTO.getAdScreenId() );
		return ( AdTabDTO ) getAdTabDao().findByName( AdTabDao.TRANSFORM_ADTABDTO, adScreen, name );
	}

	@Override
	protected void handleFillInitialValuesForField(AdFieldDTO adFieldDTO) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	protected void handleFillInitialValuesForTab(AdTabDTO adTabDTO) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	protected AdTabDTO[] handleGetTabs(AdScreenDTO adScreenDTO, Integer depth) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}